<?php
namespace Admin\Controller;
use Think\Page;
class AdminController extends CommonController {
    public function lst(){
		
		$mod=M("admin");
		//$User = M('User'); // 实例化User对象
		$count      = $mod->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,1);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$Page->setConfig('first','首页');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('end','共有{%TOTAL_PAGE%}页');
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $mod->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	
	public function add(){
		$mod=M('admin');
		if(IS_POST){
			$_POST['password']=md5($_POST['password']);
		if($mod->create()){
			if($mod->add()){
				$this->success("添加用户成功！",U("lst"));
			}else{
				$this->error("添加用户失败！");
			}
			
		}else{
			$this->error($mod->getError());
		}
		return;
		}
		$this->display();
	}
	
	public function edit(){
		$mod= M('admin');
		$admin = $mod->find(I('id'));
		if(IS_POST){
			if($mod->create()){
					if(I('password')){
					$mod->password=md5($_POST['password']);
				}else{
					$mod->password=$admin['password'];
				}
				if($mod->save()){
					$this->success("修改信息成功！",U('lst'));
				}else{
					
				
					$this->error("修改信息失败！");
				}
			}else{
				$this->error($mod->getError());
			}
			return;
		}
				
			


		$this->assign("admin",$admin);
		$this->display();
	}
	
	public function del(){
		$mod=D('admin');
		$id = I('id');
			if($mod->delete(I('id'))){
				$this->success("删除管理员信息成功！",U('lst'));
			}else{
				$this->error("删除管理员信息失败！");
			}
			
		}
		
			public function logout(){
			session(null);
				$this->success("退出成功！",U("Login/index"));
			
		}
	
}